/*
 * utilities.js
 *
 * A collection of useful utility functions required by many parts of the
 * application.
 *
 * @author Tanya L. Crenshaw
 * Edited By: Ben Co, Ben Gadbois, Michael Lietke, Julia Weimar-East
 */

/* From JavaScript patterns by S. Stefanov,
 *    "As the complexity of a program grows and some parts of the code
 * get split into different files, it becomes unsafe to just assume that
 * your code is first.  Therefore, before adding a property or creating
 * a namespace, it's best to check first that it doesn't already exist"
 */
var cs441GoogleMapsViz = cs441GoogleMapsViz || {};

/*
 * cs441GoogleMapsViz.addEvent()
 *
 * A utility function adapted from Google Code university to
 * simplify the browser-dependent act of adding events to
 * html elements.  Given an element, an event, and a function
 * this function checks the browser capability, and uses the
 * correct "add listener" or "attach event" function to
 * bind the function to the event for the element.
 *
 * @param el The html element.
 * @param evt A string representing the event.  e.g. 'click'
 * @param fn The function that will be invoked when the given
 * event 'evt' is experienced by the given element 'el'.
 *
 * @return void
 */
cs441GoogleMapsViz.addEvent = function(el, evt, fn) {
        if(el.addEventListener) {
                el.addEventListener(evt, fn, false);
        } else if(el.attachEvent) {
                el.attachEvent('on' + evt, fn);
        }
};

/* cs441GoogleMapsViz.forEach()
 *
 * A utility function adopted from "Eloquent JavaScript" that
 * accepts an action and applies that action to each element in
 * an array.  This helps to abstract the tedium of for-loops.
 *
 * @param array The array of elements to operate upon.
 * @param action The action to perform on each element.
 *
 * @return void
 */
cs441GoogleMapsViz.forEach = function(array, action) {

        for(var i = 0; i < array.length; i++) {
                action(array[i]);
        }

};

/*
 * Encrypted IDs for all of the data graciously scraped
 * and converted by the CS441 students, organized
 * by layer and then by state.
 *
 * Each table has an owner listed.  If you are having problems with a
 * table, contact the owner.
 *
 * Each table has been tested within the current cs441
 * example source.  If a table is marked as "Working"
 * that means that it displays the state's data when
 * filtered by the regionFilterMenu.
 *
 * If you want to access a particular table via the Google web interface,
 * just concatenate the particular encrypted id to this url:
 * 
 * https://www.google.com/fusiontables/data?docid=
 *
 *
 */
cs441GoogleMapsViz.tables = {
        "schools" : {
                "AK" : "1BnS-hHq88C6ZFwVxbbPO_y60Z6YgDmT0xBV0yE8", // TLC: 10/29.  Meyer et al.   Broken.  Does not contain Alaska?
                "AZ" : "1qfh_Ang7T1SZU_ksFuikFChk11QiFgH7N3VwX38", // TLC: 10/27.  Mueller et al. Working.
                "CA" : "1BnS-hHq88C6ZFwVxbbPO_y60Z6YgDmT0xBV0yE8", // TLC: 10/29.  Meyer et al.   Working.
                "CO" : "1xJmJoCeiuTjT6hZzk50RGnz345peFTiGrxN2CvI", // TLC: 10/28.  Gadbois et al. Colorado, Illinois, Nebraska, Utah.  Working.  Geocoded incorrectly.
                "HI" : "1eJtzOTidfcg7lZzt8EXmhZ44KWLT1XwbZv_ZhfA", // TLC: 10/27.  Mueller et al. Working.
                "ID" : "1BnS-hHq88C6ZFwVxbbPO_y60Z6YgDmT0xBV0yE8", // TLC: 10/29.  Meyer et al.   Broken.  Does not contain Idaho?
                "IL" : "1VoGRgwZJxFUoUEVPJp69fGbOHn58-G4TZM47CVk", // TLC: 10/28.  Gadbois et al. Colorado, Illinois, Nebraska, Utah.  Working.  Geocoded incorrectly.
                "ME" : "15ubOSYr_dPPg1jogrxP-MsxyDufp3LQrdbPn2qk", // TLC: 10/28.  Garcia et al.  Working.
                "MN" : "1BnS-hHq88C6ZFwVxbbPO_y60Z6YgDmT0xBV0yE8", // TLC: 10/29.  Meyer et al.   Working.
                "NE" : "1w_LgV77ewyPHTYpjpjePguyGmdS1LjBf4l9fCa0", // TLC: 10/28.  Gadbois et al. Colorado, Illinois, Nebraska, Utah.  Working.  Geocoded incorrectly.
                "NM" : "1G2ym2jYBbZBUUDuVUFza4fcyRS4wWK-kCTR7rrE", // TLC: 10/27.  Mueller et al. Working.
                "NV" : "18NwuT4jTzpHZmnBlFc0mGRRbfwTdSCvmiwmjmkc", // TLC: 10/27.  Garcia et al.  Working.
                "NY" : "14BrOdBhBKJOAEoMMU_jXXSNVY3nFZTglWjYg8UA", // TLC: 10/27.  Mueller et al. Working.
                "OR" : "1YcgR53xYb2OQm77NNyYfMh7-nwkrtZxoig7U_ms", // TLC: 10/27.  Crenshaw.      Working.
                "TX" : "1yhOPuREkVMFiEoGzvXvuSzLWTQ5Bz2hkqBv3ydU", // TLC: 10/28.  Garcia et al.  Working.
                "UT" : "1GyWQUGxxIlnv7tQawGVE5NJ5Po3GYpetsLjjmu0", // TLC: 10/28.  Gadbois et al. Colorado, Illinois, Nebraska, Utah.  Working. 
                "WA" : "11iPDAUiYhncL2iyoJ6fvPrmjv1iPSt55XjIGcbM", // TLC: 10/27.  Garcia et al.  Working.  Geocoded incorrectly.
        },

        "zips" : {
                "AK" : "1FNPaWfRBUgGPugh0TJ5kHzs5W2E9ZdyrvNg91Ms", // TLC: 10/29.  Meyer et al.  California, Idaho, Alaska.  Working.
                "AZ" : "1b8vDRj6_8iPCjaIgpqy-ytX-VIgRLfW0BCpcKT8", // TLC: 10/27.  Mueller et al.  Working.
                "CA" : "1FNPaWfRBUgGPugh0TJ5kHzs5W2E9ZdyrvNg91Ms", // TLC: 10/29.  Meyer et al.  California, Idaho, Alaska.  Working.
                "CO" : "1pGyFVJ3u9C9jEQGpl_aba7r7uWiIVBDAUADFilM", // TLC: 10/27.  Gadbois et al.  Working.  
                "HI" : "1aAd9SN0kmg7DtK1jhuh3M5C4y0OYihgdhS_Z6IA", // TLC: 10/27.  Mueller et al.  Working.
                "ID" : "1FNPaWfRBUgGPugh0TJ5kHzs5W2E9ZdyrvNg91Ms", // TLC: 10/27.  Meyer et al.  California, Idaho, Alaska.  Working.
                "IL" : "1cKRKxLql_yySfS0VKJR8l3FRnv9QYmO5hnvv3Cs", // TLC: 10/27.  Gadbois et al.  Working.
                "ME" : "11t_bjHCP5fSU1YdUjcPwnjT8TKsaYxmpmkKs9tk", // TLC: 10/27.  Garcia et al.   Working.
                "MN" : "1pkW7EOA4TaoLrR637OeKsR4_WG2EERyqRhSMKJs", // TLC: 10/29.  Meyer et al.  Oregon and Minnesota.
                "NE" : "168QyhgFvpITQmKeXdQ3NQSNkgDC55T4B1PUJKhk", // TLC: 10/27.  Gadbois et al.  Working.
                "NM" : "1bFE-rO_gsHyDp7e5yKUirHKL5qv3KU6wLKJk_kE", // TLC: 10/27.  Mueller et al.  Working.
                "NV" : "1NIpIVPjf8n-ZCEWnZoLl3uCJApMvOukFrYzdCZ0", // TLC: 10/27.  Garcia et al.   Working.
                "NY" : "1fPNF_U6oLbIresIP7azTx60-_z2yPa7MXmQJ3OM", // TLC: 10/27.  Mueller et al.  Working.
                "OR" : "1U6n-9O6I9q4qG10uNcNrCAlvactfL7O07IVPLbU", // TLC: 10/27.  Crenshaw.       Working.
                "TX" : "1VM5jgrP_ROg5kFcTpwpJuw-fDpgvaSeQ4JAgEvw", // TLC: 10/27.  Garcia et al.   Working.
                "UT" : "1ImtKIQYOTlEFgy1oLF8WeygB3antpx8Nx-qwQYU", // TLC: 10/27.  Gadbois et al.  Working.
                "WA" : "1jNwC6KeDC3NnVlReslJZB8VvujKjyxQhDs7o5Tc", // TLC: 10/27.  Garcia et al.   Working.
        },

        "students" : {
                "Enrolled" : "1MpKoIu0SIVNvEILq-HqPHOYbK67akZU-h3jSRHw", // Crenshaw
                "Set1" : "1S4AFgnDa4IVeNTgXm5qjoVqUSB_GD0OPkRgktME"       // Meyer
        }

};

/*
 * cs441GoogleMapsViz.convertZipToState
 * 
 * Given a zip code, this function returns the state abbreviation that corresponds 
 * to the zip code.
 * 
 * @param zip An five digit integer representing the zip that needs to be converted. 
 * 
 * @return state The state abbreviation
 */
cs441GoogleMapsViz.convertZipToState = function(zip) {
	zip = zip + "";
	zip2 = parseInt(zip.substring(0,2));
	zip3 = parseInt(zip.substring(0,3));
	var state = "";
	if ((zip3 >= 995) && (zip3 <= 999)) { state = "AK";}
	else if ((zip2 >= 85) && (zip2 <= 86)) { state = "AZ";}
	else if ((zip3 >= 900) && (zip3 <= 961)) { state = "CA";}
	else if ((zip2 >= 80) && (zip2 <= 81)) { state = "CO";}
	else if ((zip3 >= 967) && (zip3 <= 968)) { state = "HI";}
	else if ((zip3 >= 832) && (zip3 <= 839)) { state = "ID";}
	else if ((zip2 >= 60) && (zip2 <= 62)) { state = "IL";}
	else if ((zip3 >= 39) && (zip3 <= 49)) { state = "ME";}
	else if ((zip3 >= 550) && (zip3 <= 567)) { state = "MN";}
	else if ((zip2 >= 68) && (zip2 <= 69)) { state = "NE";}
	else if ((zip3 >= 870) && (zip3 <= 884)){ state = "NM";}
	else if ((zip3 >= 889) && (zip3 <= 899)) { state = "NV";}
	else if (((zip2 >= 10) && (zip2 <= 14)) || zip3 == 5) { state = "NY";}
	else if (zip2 == 97) { state = "OR";}
	else if (((zip2 >= 75) && (zip2 <= 79)) || (zip3 == 885)) { state = "TX";}
	else if (zip2 == 84) { state = "UT";}
	else if ((zip3 >= 980) && (zip3 <= 994)){ state = "WA";}
	else{
		//Do nothing
	}
	return state;
}